const {
  api
} = require("./util");
var util = require('./util')

const baseURL = 'https://tp.needyoutd.com/bs';
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';


function getSession() {
  return new Promise((resolve, reject) => {
    // 登录
    wx.login({
      success: async function (res) {
        const result = await API.login(res.code)
        wx.setStorageSync('session', result.data.session)
        setTimeout(() => {
          resolve(true)
        }, 0)
      },
      fail: err => {
        console.log(err);
        reject(err)
      }
    })
  })
}

function request(parama) {
  let _this = this;
  return new Promise((resolve, reject) => {
    let header = {
      // 'content-type': 'application/json',
      'session': wx.getStorageSync('session')
    };
    wx.request({
      url: baseURL + parama.url,
      data: parama.methods === POST ? JSON.stringify(parama.data) : parama.data,
      method: parama.methods,
      header: header,

      success: res => {
        resolve(res.data)
      },
      fail: err => {
        console.error(`wx.request fail`, err)
        reject({
          err: err
        })
      }
    })
  })
}


function uploadImg(img) {
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: 'https://tp.needyoutd.com/bs/file/upload_image?session' + wx.getStorageSync('session'), //仅为示例，非真实的接口地址
      filePath: img,
      name: 'image',
      header: {
        session: wx.getStorageSync('session')
      },
      // formData: {
      //   'user': 'test'
      // },
      success(res) {
        resolve(JSON.parse(res.data))
      }
    })
  })
}

const API = {

  // :()=>{
  //   return request({
  //     url:'',
  //     method:''   
  //   })
  // }

  login: (code) => {
    return request({
      url: '/user/login?code=' + code,
      methods: 'GET'
    })
  },

  // 设置投票
  setVote: (data) => {
    return request({
      url: '/new_vote/set_vote?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  //获取投票详情
  getVote: (data) => {
    return request({
      url: '/new_vote/get_vote?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  //获取投票选项
  getVoteOptions: (data) => {
    return request({
      url: '/new_vote/query_option?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 投票
  postCastVote: (data) => {
    return request({
      url: '/new_vote/cast_vote?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 发送评论
  postVoteComment: (data) => {
    return request({
      url: '/new_vote/vote_comment?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 查看评论
  postQueryComment: (data) => {
    return request({
      url: '/new_vote/query_vote_comment?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },
  // 我的页面获取数据
  postQueryVote: () => {
    return request({
      url: '/new_vote/query_user_data?session=' + wx.getStorageSync('session'),
      methods: 'POST',
    })
  },


  // 获取首页数据
  postIndexVote: () => {
    return request({
      url: '/vote/query_vote',
      methods: 'POST',
    })
  },

  // 获取排行榜
  postRank: (data) => {
    return request({
      url: '/new_vote/get_rank?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 查询单个选项详情
  postOptionDetail: (data) => {
    return request({
      url: '/new_vote/get_option?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 查询记录
  queryUserDetail: (data) => {
    return request({
      url: '/new_vote/query_user_detail?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 报名
  postEnroll: (data) => {
    return request({
      url: '/new_vote/enroll?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 更新分组
  updateGroup: (data) => {
    return request({
      url: '/new_vote/update_group?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 上传音频视频
  uploadFile: (data) => {
    return request({
      url: '/file/upload_file',
      methods: 'POST',
      data: data
    })
  },

  // 查询选手信息 
  adminQueryOption: (data) => {
    return request({
      url: '/new_vote/admin_query_option?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },


  // 通过
  passOption: (data) => {
    return request({
      url: '/vote/pass_option?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 拒绝
  rejectOption: (data) => {
    return request({
      url: '/vote/reject_option?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 编辑投票
  updateVote: (data) => {
    return request({
      url: '/new_vote/update_vote?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 导出投票数据
  exportData: (data) => {
    return request({
      url: '/new_vote/export_data?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 复制投票
  copyVote: (data) => {
    return request({
      url: '/new_vote/copy_vote?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 复制投票
  getCode: (data) => {
    return request({
      url: '/vote/get_code?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 搜索作品
  searchOption: (data) => {
    return request({
      url: '/vote/search_option?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 根据分组名查询选手信息
  queryGroupOption: (data) => {
    return request({
      url: '/new_vote/query_group_option?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 刪除評論
  deleteVoteComment: (data) => {
    return request({
      url: '/new_vote/delete_vote_comment?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 回复评论
  replyVoteComment: (data) => {
    return request({
      url: '/new_vote/reply_vote_comment?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },


  // webview获取链接
  getUrl: (data) => {
    return request({
      url: '/admin/get_url',
      methods: 'POST',
      data: data
    })
  },

  // 发送反馈
  sendReport: (data) => {
    return request({
      url: '/new_vote/send_report?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 发送反馈
  updateOption: (data) => {
    return request({
      url: '/new_vote/update_option?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 管理审核
  adminQueryVote: (data) => {
    return request({
      url: '/vote/admin_query_vote?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 上热门
  changeHot: (data) => {
    return request({
      url: '/vote/change_hot?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 上推荐
  changeRecommend: (data) => {
    return request({
      url: '/vote/change_recommend?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 是否管理员
  judgeAdmin: (data) => {
    return request({
      url: '/user/judge_admin?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 设置用户信息
  setUserInfo: (data) => {
    return request({
      url: '/user/set_user?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 浏览量

  addClick: (data) => {
    return request({
      url: '/vote/add_click?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 搜索投票
  searchVote: (data) => {
    return request({
      url: '/new_vote/search_vote?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 置顶
  changeUp: (data) => {
    return request({
      url: '/new_vote/change_up?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 首页查询额外跳转应用(无需session)
  queryExtendApp: (data) => {
    return request({
      url: '/admin/query_extend_app',
      methods: 'POST',
      data: data
    })
  },

  // 新增额外跳转应用(无需session)
  addExtendApp: (data) => {
    return request({
      url: '/admin/add_extend_app',
      methods: 'POST',
      data: data
    })
  },

  // 后台管理页面查询首页额外跳转应用(无需session)
  adminQueryExtendApp: (data) => {
    return request({
      url: '/admin/admin_query_extend_app',
      methods: 'POST',
      data: data
    })
  },

  // 修改额外跳转应用(无需session)
  alterExtendApp: (data) => {
    return request({
      url: '/admin/alter_extend_app',
      methods: 'POST',
      data: data
    })
  },

  // 修改额外跳转应用(无需session)
  deleteExtendApp: (data) => {
    return request({
      url: '/admin/delete_extend_app',
      methods: 'POST',
      data: data
    })
  },

  //管理员新增报名选项
  adminCreateOption: (data) => {
    return request({
      url: '/new_vote/admin_create_option?session=' + wx.getStorageSync('session'),
      methods: 'POST',
      data: data
    })
  },

  // 管理员插入假投票数据(无需session)
  changeFakeVo: (data) => {
    return request({
      url: '/vote/change_fake_vo',
      methods: 'POST',
      data: data
    })
  },

  //获取用户openId
  getOpenid: (data) => {
    return request({
      url: '/user/get_openid?session=' + wx.getStorageSync('session'),
      methods: 'POST',
    })
  },



}



function isIos() {
  return new Promise((resolve, reject) => {
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        resolve(res.platform)
      }
    })
  })
}

//支付
async function wxpay(data) {
  wx.showLoading({})
  return new Promise((resolve, reject) => {
    isIos().then(plaform => {
      console.log(plaform, 'res')
      if (plaform == 'ios') {
        wx.showModal({
          title: '提示',
          content: 'IOS不支持',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        reject()
        wx.hideLoading({
          success: (res) => {},
        })
      } else {
        API.createPay(data).then(res => {
          console.log(res)
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: 'MD5',
            paySign: res.data.paySign,
            success(result) {
              resolve({
                res: result,
                trade_no: res.data.trade_no
              })
            },
            fail(res) {
              reject()
            }
          })
          wx.hideLoading({
            success: (res) => {},
          })
        })
      }
    })
  })
}



module.exports = {
  API: API,
  uploadImg,
  getSession,
  wxpay,
}